home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / TOGGLE.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  7.4 KB  |  216 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.input.inout_draggable;
  4. import sub_arctic.input.pressable;
  5. import sub_arctic.input.callback_object;
  6. import sub_arctic.input.event;
  7.  
  8. import sub_arctic.output.loaded_image;
  9. import sub_arctic.output.drawable;
  10.  
  11.  
  12. /**
  13.  * This class is a common superclass for checkbox and radio_button. This
  14.  * object currently may be instantiated to get the old subarctic look,
  15.  * but this may go away in the future.
  16.  * 
  17.  * @author Scott Hudson
  18.  */
  19. public class toggle extends multi_button implements inout_draggable, pressable {
  20.  
  21.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  22.  
  23.   /** 
  24.    * Link for circular list of radio button group.  If we are not in a 
  25.    * group, this will refer to ourselves.
  26.    */
  27.   protected toggle _group_link;
  28.  
  29.   /** 
  30.    * Link for circular list of radio button group.  If we are not in a 
  31.    * group, this will refer to ourselves.
  32.    * @return toggle our link in the circular group list.
  33.    */
  34.   public toggle group_link() {return _group_link;}
  35.  
  36.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  37.  
  38.   /** 
  39.    * Full constructor.  The two image sets should each have two entries.  The
  40.    * entries at index 0 provide appearance for the 0 (off) state and the 
  41.    * transition between state 0 and state 1 respectively (index 1 similarly
  42.    * maps to state 1).  The transition set may be coded as null in which
  43.    * case the next state image is used when a transition is needed.  Otherwise
  44.    * the two image sets must be the same size.  
  45.    *
  46.    * @param int              x           x position of the toggle.
  47.    * @param int              y           y position of the toggle.
  48.    * @param loaded_image[]   st_looks    set of images for normal looks.
  49.    * @param loaded_image[]   trans_looks set of images for looks during 
  50.    *                                     transitions between states (can be 
  51.    *                                     null to denote not special transition 
  52.    *                                     appearances).
  53.    * @param toggle           group_with  toggle to group this with to get 
  54.    *                                     radio_button semantics (or null for 
  55.    *                                     no grouping).
  56.    * @param callback_object  call_obj    object that gets callbacks about 
  57.    *                                     state changes.
  58.    */
  59.   public toggle(
  60.     int              x,
  61.     int              y,
  62.     loaded_image[]   st_looks, 
  63.     loaded_image[]   trans_looks, 
  64.     toggle           group_with,
  65.     callback_object  call_obj) 
  66. {
  67.       super(x, y, st_looks, trans_looks, call_obj);
  68.  
  69.       /* start with no group */
  70.       _group_link = this;
  71.  
  72.       /* add to group if requested */
  73.       if (group_with != null) add_to_group_of(group_with);
  74.  
  75.       /* make sure the image sets are usable */
  76.       if (st_looks == null || st_looks.length != 2)
  77.     throw new sub_arctic_error("Wrong number of images passed to toggle()");
  78.  
  79.       if (trans_looks != null && trans_looks.length != st_looks.length)
  80.     throw new sub_arctic_error("Normal and transition image sets of unequal " + 
  81.                 "size passed to toggle()");
  82.     }
  83.  
  84.    //had:
  85.    //* @exception bad_value if wrong number of images are passed
  86.  
  87.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  88.  
  89.   /** 
  90.    * Default appearance constructor. 
  91.    *
  92.    * @param int             x        x position of the toggle.
  93.    * @param int             y        y position of the toggle.
  94.    * @param callback_object call_obj object that gets callbacks about 
  95.    *                                 state changes.
  96.    */
  97.   public toggle( int x, int y, callback_object call_obj) 
  98. {
  99.       this(x,y, default_look(),default_transition(), null, call_obj);
  100.     }
  101.  
  102.    //had:
  103.    //* @exception bad_value PROPAGATED.
  104.    //* @exception general PROPAGATED.
  105.  
  106.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  107.  
  108.   /** 
  109.    * Override set_cur_state to reset other group members (if any) if we
  110.    * are being set (put in state 1).
  111.    * @param int st new state.
  112.    */
  113.   public void set_cur_state(int st)
  114.     {
  115.       /* let super class set our local state */
  116.       super.set_cur_state(st);
  117.  
  118.       /* if we are being set, run trough rest of group (if any) and reset */
  119.       if (cur_state() == 1)
  120.         for (toggle t = group_link(); t != this; t = t.group_link())
  121.       t.set_cur_state(0);
  122.     }
  123.  
  124.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  125.  
  126.   /** 
  127.    * Override next_state to provide group behavior.  If we are not in a 
  128.    * group then we just toggle.  However, if we are in a group then we 
  129.    * transition from off to on, but not on to off (we get reset by another
  130.    * group member being set).
  131.    * @return int the state we will enter if activated again.
  132.    */
  133.   public int next_state()
  134.     {
  135.       /* if there is no group then we just use the default */
  136.       if (_group_link == this)
  137.     return super.next_state();
  138.  
  139.       /* otherwise transitions are 0 to 1 and 1 to 1 */
  140.       return 1;
  141.     }
  142.  
  143.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  144.  
  145.   /** 
  146.    * Return the previous toggle in our circular group list. 
  147.    * @return toggle the toggle before us in the group list.
  148.    */
  149.   protected toggle group_prev()
  150.     {
  151.       /* find the toggle in the circular group list that points to us */
  152.       for (toggle t = _group_link;  ; t = t._group_link)
  153.     if (t._group_link == this) return t;
  154.     }
  155.  
  156.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  157.  
  158.   /** Drop us from our current group */
  159.   public void drop_from_group()
  160.     {
  161.       /* take us out of the circular list */
  162.       group_prev()._group_link = _group_link;
  163.  
  164.       /* make us a singleton group */
  165.       _group_link = this;
  166.     }
  167.  
  168.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  169.  
  170.   /** 
  171.    * Add us to the group the given toggle is in.
  172.    * @param toggle other the toggle whose group we are joining.
  173.    */
  174.   public void add_to_group_of(toggle other)
  175.     {
  176.       /* make sure we are not in some other group */
  177.       drop_from_group();
  178.  
  179.       /* put us in other's group */
  180.       _group_link = other._group_link;
  181.       other._group_link = this;
  182.     }
  183.  
  184.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  185.  
  186.   /** Images for default normal appearance of a toggle. */
  187.   protected static loaded_image[] _default_look = std.toggle_image_set();
  188.  
  189.   /** Images for default normal appearance of a toggle. */
  190.   public static loaded_image[] default_look() { return _default_look; }
  191.  
  192.   /** Images for default transition appearance of a toggle.  By default this
  193.    *  is null and signifies that we just use the next state image for the 
  194.    *  transition.
  195.    */
  196.   public static loaded_image[] default_transition() { return null; }
  197.  
  198.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  199. }
  200. /*=========================== COPYRIGHT NOTICE ===========================
  201.  
  202. This file is part of the subArctic user interface toolkit.
  203.  
  204. Copyright (c) 1996 Scott Hudson and Ian Smith
  205. All rights reserved.
  206.  
  207. The subArctic system is freely available for most uses under the terms
  208. and conditions described in 
  209.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  210. and appearing in full in the lib/interactor.java source file.
  211.  
  212. The current release and additional information about this software can be 
  213. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  214.  
  215. ========================================================================*/
  216.